package com.wanmait.schedule.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageInfo;
import com.wanmait.schedule.entity.*;
import com.wanmait.schedule.service.ApplyService;
import com.wanmait.schedule.service.CompanyService;
import com.wanmait.schedule.service.CompanyTypeService;
import com.wanmait.schedule.service.UserinfoService;
import com.wanmait.schedule.util.JWTUtils;
import com.wanmait.schedule.util.Result;
import org.apache.commons.io.FilenameUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import static com.wanmait.schedule.util.Result.error;


/**
 * <p>
 * 企业表 前端控制器
 * </p>
 *
 * @author wanmait
 * @since 2022-03-02
 */
@RestController
@RequestMapping("/company")
public class CompanyController {

    @Resource
    private CompanyService companyService;

    @Resource
    private UserinfoService userinfoService;

    @Resource
    private CompanyTypeService companyTypeService;

    @Resource
    private ApplyService applyService;

    /*
    * 2022.03.10
    * 王志羽
    * 创建企业
    * */
    @PostMapping("/personal/CreateCompany")
    public Result createCompany(Company company, MultipartFile file) {

        System.out.println("file::"+file);
        if(file != null && !file.isEmpty()){
            if(file.getSize()>1024*1024*2){
                return Result.error();
            }
            String ext = FilenameUtils.getExtension(file.getOriginalFilename());
            if (!",jpg,jpeg,gif,webp,png,".contains(","+ext.toLowerCase()+",")){
                return Result.error();
            }
            try {
                String path = ResourceUtils.getURL("classpath:").getPath()+"/static/uploadFiles";
                String fileName = UUID.randomUUID().toString().replaceAll("-","")+"."+ext;
                file.transferTo(new File(path,fileName));
                company.setLogo(fileName);
                System.out.println("company::"+company);
            } catch (Exception e) {
                e.printStackTrace();
                return Result.error();
            }

        }
        company.setCount(100);
        companyService.save(company);
        return Result.success(company);
    }
    /*
    * 2022.3.14
    * 王志羽
    * 申请加入公司，发出申请
    * */
    @PostMapping("personal/JoinCompany")
    public Result joinCompany(Integer companyId,Integer userinfoId) {
//        System.out.println("id::"+companyId);

        Apply apply = new Apply();
        apply.setCompanyId(companyId);
        apply.setUserinfoId(userinfoId);
        applyService.save(apply);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("apply",apply);
        return Result.success(jsonObject);
    }

    /*
    * 2022.3.17
    * 王志羽
    * 初始化
    * 是否重复申请
    * */

    @GetMapping("personal/initCompany")
    public Result initCompany(Integer pageNum,Integer pageSize){
        PageInfo<Company> companies = companyService.findPage(pageNum,pageSize);
        List<Apply> applies = applyService.findAll();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("companies",companies);
        jsonObject.put("applies",applies);
        return Result.success(jsonObject);
    }


    /*
    * 2022.3.15
    * 王志羽
    * 通过企业名称搜索企业
    * */
    @GetMapping("personal/findCompanyByName")
    public Result findCompanyByName(String name){
        System.out.println("name::"+name);
        return Result.success(companyService.selectCompang(name));
    }

// ----------------------------------⬇老板企业-----------------------------------------------
    //老板修改账号页面是初始化数据
    @GetMapping("leader/initData")
    public Result initData(HttpServletRequest request){
        CompanyUser companyUser = (CompanyUser) request.getAttribute("companyUser");
        Integer companyId = companyUser.getCompanyId();
        Company company = companyService.initData(companyId);
        return Result.success(company);
    }
    //老板修改账号页面是初始化数据
    @GetMapping("leader/initCompanyType")
    public Result initCompanyTpe(){
        List<CompanyType> companyTypes = companyService.initCompanyType();
        return Result.success(companyTypes);
    }
    //确定修改
    @PostMapping("leader/companyEdit")
    public Result companyEdit(Company company,MultipartFile file,HttpServletRequest request) {
        CompanyUser companyUser = (CompanyUser)request.getAttribute("companyUser");
        Integer companyId = companyUser.getCompanyId();
        Boolean flag = companyService.companyEdit(company,file,companyId);
        if (flag){
            return Result.success("修改成功");
        }
        return Result.error("修改失败");
    }
}
